package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.DevParameterDao;
import com.sugon.entity.DevParameterEntity;
import com.sugon.entity.DevTypeEntity;
import com.sugon.entity.FactoryTypeEntity;
import com.sugon.service.DevParameterService;
import com.sugon.service.DevTypeService;
import com.sugon.service.FactoryTypeService;
import com.sugon.utils.RRException;
import com.sugon.utils.excel.ExcelImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备参数表Service实现类
 *
 * @author YuXD
 * @date 2020-05-26 16:24:56
 */
@Service("devParameterService")
public class DevParameterServiceImpl implements DevParameterService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private DevParameterDao devParameterDao;
    @Autowired
    private DevTypeService devTypeService;
    @Autowired
    private FactoryTypeService factoryTypeService;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevParameterEntity queryObject(String id) {
        return devParameterDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevParameterEntity> queryList(Map<String, Object> map) {
        return devParameterDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devParameterDao.queryTotal(map);
    }

    /**
     * queryObject
     *
     * @param devCode   设备编码
     * @param paramName 参数名称
     * @return
     */
    @Override
    public DevParameterEntity queryObject(String devCode, String paramName) {
        Map<String, Object> param = new HashMap<>();
        param.put("devCode", devCode);
        param.put("paramName", paramName);
        List<DevParameterEntity> devParams = devParameterDao.queryDevParam(param);
        return CollectionUtil.isNotEmpty(devParams) ? devParams.get(0) : null;
    }

    /**
     * save
     *
     * @param devParameter 实体
     * @return
     */
    @Override
    public int save(DevParameterEntity devParameter) {
        devParameter.setId(RandomUtil.randomString(32));
        devParameter.setDelFlag(1);
        // 暂时设置默认值
        devParameter.setThresholdMin(-100.0);
        devParameter.setThresholdMax(2000.0);
        return devParameterDao.save(devParameter);
    }

    /**
     * update
     *
     * @param devParameter 实体
     * @return
     */
    @Override
    public int update(DevParameterEntity devParameter) {
        return devParameterDao.update(devParameter);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devParameterDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devParameterDao.deleteBatch(ids);
    }

    /**
     * imports
     *
     * @param file
     * @return
     */
    @Override
    public int imports(MultipartFile file) {
        // 声明导入行数
        int num = 0;
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        List<String[]> list = ExcelImport.getExcelData(file);
        // 导入模板列表行数不得低于两行
        if (CollectionUtil.isEmpty(list) || list.size() < 2) {
            return num;
        }
        // 获取导入模板首行
        String[] paramNames = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            String[] ftArr = list.get(i);
            if (ftArr.length < 3) {
                continue;
            }
            if (StrUtil.isEmpty(ftArr[0]) || StrUtil.isEmpty(ftArr[1])) {
                log.info("设备类型和厂家不能为空");
                continue;
            }
            // 校验下设备类型是否存在
            DevTypeEntity devType = devTypeService.queryObjectByCode(ftArr[0]);
            if (devType == null) {
                log.info("设备类型[{}]尚未添加，请添加后再进行设置", ftArr[0]);
                continue;
            }
            // 校验下该公司是否已存在
            Map<String, Object> param = new HashMap<>();
            param.put("name", ftArr[1]);
            param.put("type", 0);
            List<FactoryTypeEntity> factoryTypes = factoryTypeService.queryList(param);
            if (CollectionUtil.isEmpty(factoryTypes)) {
                log.info("供应厂商[{}]尚未添加，请添加后再进行设置", ftArr[1]);
                continue;
            }
            DevParameterEntity devParam = new DevParameterEntity();
            devParam.setDevType(ftArr[0]);
            devParam.setDevTypeName(devType.getTypeName());
            devParam.setFaName(ftArr[1]);
            devParam.setFaCode(factoryTypes.get(0).getFaCode());
            for (int j = 2; j < ftArr.length; j++) {
                devParam.setParamName(paramNames[j]);
                devParam.setParamDesc(ftArr[j]);
                save(devParam);
            }
            num++;
        }
        return num;
    }
}
